-- This function is run last and is a good place to configuring
-- augroups/autocommands and custom filetypes also this just pure lua so
-- anything that doesn't fit in the normal config locations above can go here
local utils = require "astrocore"
local is_available = utils.is_available
-- return function()
-- Set up custom filetypes
-- vim.filetype.add {
--   extension = {
--     foo = "fooscript",
--   },
--   filename = {
--     ["Foofile"] = "fooscript",
--   },
--   pattern = {
--     ["~/%.config/foo/.*"] = "fooscript",
--   },
-- }
require "user.utils.autocmds"
require "user.utils.keymap"
-- local utils = require "astronvim.utils"
-- local is_available = utils.is_available
-- local lsp_mappings = require("astronvim.utils").empty_map_table()

local opt = { noremap = true, silent = true }
-- vim.keymap.set({ "n", "i", "v" }, "<Left>", "<Nop>", opt)
-- vim.keymap.set({ "n", "i", "v" }, "<Right>", "<Nop>", opt)
-- vim.keymap.set({ "n", "i", "v" }, "<Up>", "<Nop>", opt)
-- vim.keymap.set({ "n", "i", "v" }, "<Down>", "<Nop>", opt)
-- vim.keymap.set({ "n", "i", "v" }, "<ESC>", "<Nop>")
-- vim.keymap.set({ "n", "i", "v" }, "<BS>", "<Nop>", opt)
-- vim.keymap.set({ "v" }, "p", '"0p', opt)
-- vim.keymap.set({ "v" }, "P", '"0P', opt)
-- :w !sudo tee %
vim.api_nvim_set_keymap("c", "w!!", "<esc>:lua require'utils'.sudo_write()<CR>", { silent = true })
-- :cnoremap <C-A> <Home>
-- :cnoremap <C-F> <Right>
-- :cnoremap <C-B> <Left>
-- :cnoremap <Esc>b <S-Left>
-- :cnoremap <Esc>f <S-Right>
vim.cmd [[cnoremap <C-a> <Home>]]
vim.cmd [[cnoremap <C-f> <Right>]]
vim.cmd [[cnoremap <C-o> <C-f>]]
vim.cmd [[cnoremap <C-b> <Left>]]
vim.cmd [[cnoremap <A-f> <S-Right>]]
vim.cmd [[cnoremap <A-b> <S-Left>]]

vim.cmd [[inoremap <C-a> <Home>]]
vim.cmd [[inoremap <C-e> <End>]]
vim.cmd [[inoremap <C-f> <Right>]]
vim.cmd [[inoremap <C-b> <Left>]]
-- vim.keymap.set({ "n", "v" }, "n", "nzz", opt)
-- vim.keymap.set({ "n", "v" }, "N", "Nzz", opt)
-- vim.keymap.set({ "n", "v" }, "<leader>y", '"+y', opt)
-- vim.keymap.set("n", "p", '"0p', opt)
-- vim.keymap.set("n", "P", '"0P', opt)
if vim.fn.executable "joshuto" == 1 then
  vim.keymap.set(
    { "n", "v" },
    "<leader>tj",
    require("user.utils.utils").toggle_joshuto(),
    { noremap = true, silent = true, desc = "ToggleTerm Joshuto" }
  )
end
if vim.fn.executable "yazi" == 1 then
  vim.keymap.set(
    { "n", "v" },
    "<leader>ty",
    require("user.utils.utils").toggle_yazi(),
    { noremap = true, silent = true, desc = "ToggleTerm Yazi" }
  )
end
if vim.fn.executable "lazygit" == 1 then
  vim.keymap.set(
    { "n", "v" },
    "<leader>tl",
    require("user.utils.utils").toggle_lazy_git(),
    { noremap = true, silent = true, desc = "ToggleTerm lazygit" }
  )
  vim.keymap.set(
    { "n", "v" },
    "<leader>gg",
    require("user.utils.utils").toggle_lazy_git(),
    { noremap = true, silent = true, desc = "ToggleTerm lazygit" }
  )
end

if is_available "markdown-preview.nvim" then
  vim.keymap.set(
    { "n", "v" },
    "<leader>ze",
    "<cmd>MarkdownPreviewToggle<CR>",
    { noremap = true, silent = true, desc = "Open Markdown preview" }
  )
end

if is_available "vim-dadbod-ui" then
  vim.keymap.set(
    { "n", "v" },
    "<leader>td",
    "<cmd>DBUIToggle<CR>",
    { noremap = true, silent = true, desc = "ToggleTerm DBUI" }
  )
end

-- vim.cmd [[ unmap ` ]]
vim.keymap.set({ "n", "i", "v", "t" }, "<C-`>", "<CMD>ToggleTerm size=10 direction=horizontal<cr>")
vim.keymap.set({ "n", "i", "v", "t" }, "<M-tab>", "<CMD>bnext<cr>")
vim.keymap.set({ "n", "v" }, "<leader>lT", "<CMD>TSInstallInfo<cr>", { desc = "Tree sitter Information" })
vim.keymap.set({ "n", "v" }, "<leader>lm", "<CMD>LspRestart<cr>", { desc = "Lsp restart" })
vim.keymap.set({ "n", "v" }, "<leader>lg", "<CMD>LspLog<cr>", { desc = "Show lsp log" })

vim.keymap.set(
  "n",
  "<S-TAB>",
  function() require("astrocore.buffer").nav(-(vim.v.count > 0 and vim.v.count or 1)) end,
  { desc = "Previous buffer" }
)
vim.keymap.set(
  "n",
  "<TAB>",
  function() require("astrocore.buffer").nav(vim.v.count > 0 and vim.v.count or 1) end,
  { desc = "Next buffer" }
)
vim.keymap.set(
  "n",
  "<Leader>bo",
  function() require("astrocore.buffer").close_all(true) end,
  { desc = "Close all buffers except current" }
)
-- vim.keymap.set({ "n", "v" }, "<leader>ts", "<cmd>ToggleTermSendCurrentLine<cr>", { desc = "Send Line to Terminal" })
vim.keymap.set(
  { "n", "v" },
  "<leader>j",
  "<cmd>ToggleTerm size=10 direction=horizontal<cr>",
  { noremap = true, silent = true, desc = "Toggle Below Terminal" }
)
-- not allow leader as keymap in terminal, as lazygit need to insert <space>
-- )
-- vim.api.nvim_del_keymap("n", "<leader>lD")

vim.keymap.set("n", "<leader>fd", function()
  require("telescope.builtin").diagnostics {
    -- severity_limit = vim.diagnostic.severity.INFO,
  }
end, { desc = "Search diagnostics" })

vim.keymap.set(
  "n",
  "n",
  require("user.utils.utils").better_search "n",
  { noremap = true, silent = true, desc = "Next search" }
)

vim.keymap.set(
  "n",
  "N",
  require("user.utils.utils").better_search "N",
  { noremap = true, silent = true, desc = "Next search" }
)
vim.keymap.set("i", "<C-s>", "<esc>:w<cr>a", { noremap = true, silent = true, desc = "Save file" })
-- lsp_mappings.n["<leader>lD"] = {
--   function()
--     require("telescope.builtin").diagnostics {
--       severity_limit = vim.diagnostic.severity.info,
--     }
--   end,
--   desc = "Search diagnostics",
-- }

-- vim.keymap.set("n", "<leader>rc", ":RunCode<CR>", opt)
-- vim.keymap.set("n", "<leader>rf", ":RunFile<CR>", opt)
-- vim.keymap.set("n", "<leader>rft", ":RunFile tab<CR>", opt)
-- vim.keymap.set("n", "<leader>rp", ":RunProject<CR>", opt)
-- vim.keymap.set("n", "q", ":RunClose<CR>", opt)
-- vim.keymap.set("v", "J", ":m '>+1<CR>gv=gv", opt)
-- vim.keymap.set("v", "K", ":m '<-2<CR>gv=gv", opt)
-- vim.keymap.set({ "n", "v" }, "<leader>lz", "<cmd>Lazy<cr>", opt)
-- vim.keymap.set({ "n", "v" }, "ga", ":EasyAlign")
-- vim.keymap.set({ "n", "v" }, "ga", ":EasyAlign <CR><C-x>")
-- vim.keymap.set({ "n", "v" }, "ga#", ":EasyAlign *#<CR>")
-- vim.keymap.set("n", "gt", "[b", { noremap = true, silent = false })
-- vim.keymap.set('n', '<leader>crf', ':CRFiletype<CR>', { noremap = true, silent = false })
-- vim.keymap.set('n', '<leader>crp', ':CRProjects<CR>', { noremap = true, silent = false })
-- vim.keymap.set("v", "<leader>a#", ":2,$Tabularize /#/l2<CR>", { noremap = true, silent = false })
-- vim.keymap.set("v", "<leader>a=", ":Tabularize /=<CR>", { noremap = true, silent = false })
-- vim.api.nvim_set_keymap("i", "<C-a>", "<Home>", opt)
-- vim.api.nvim_set_keymap("i", "<C-e>", "<End>", opt)
-- vim.api.nvim_set_keymap("i", "<C-f>", "<Right>", opt)
-- vim.api.nvim_set_keymap("i", "<C-b>", "<Left>", opt)
-- vim.api.nvim_set_keymap("n", "gd", "gdzz", opt)
-- vim.api.nvim_set_keymap("n", "<leader>?", ":help <C-r><C-w><CR>", opt)
-- vim.api.nvim_set_keymap("n", "<leader>?", "<cmd>help <C-r><C-w><CR>", opt)
-- vim.api.nvim_set_keymap("n", "<leader>4", "$", opt)
-- vim.api.nvim_set_keymap("n", "<leader>6", "^", opt)
-- vim.api.nvim_set_keymap("n", "dw", "diw", { noremap = true, silent = true })
-- vim.api.nvim_set_keymap("n", "cw", "ciw", { noremap = true, silent = true })
-- vim.api.nvim_set_keymap("n", "gt", ":bnext<CR>", opt)
-- vim.api.nvim_set_keymap("n", "gT", ":bprevious<CR>", opt)
-- vim.api.nvim_set_keymap("n", "<leader>bn", ":bnext<CR>", { desc = "Next Buffer", noremap = true, silent = true })
-- vim.api.nvim_set_keymap("n", "<leader>bf", ":bprevious<CR>", { desc = "Prev Buffer", noremap = true, silent = true })
-- vim.api.nvim_set_keymap("n", "<A-j>", "<cmd>m .+1<cr>==", { desc = "Move down", noremap = true, silent = true })
-- vim.api.nvim_set_keymap("n", "<A-k>", "<cmd>m .-2<cr>==", { desc = "Move up", noremap = true, silent = true })
-- vim.api.nvim_set_keymap(
--   "i",
--   "<A-j>",
--   "<esc><cmd>m .+1<cr>==gi",
--   { desc = "Move down", noremap = true, silent = true }
-- )
-- vim.api.nvim_set_keymap("i", "<A-k>", "<esc><cmd>m .-2<cr>==gi", { desc = "Move up", noremap = true, silent = true })
-- vim.api.nvim_set_keymap("v", "<A-j>", ":m '>+1<cr>gv=gv", { desc = "Move down", noremap = true, silent = true })
-- vim.api.nvim_set_keymap("v", "<A-k>", ":m '<-2<cr>gv=gv", { desc = "Move up", noremap = true, silent = true })
-- maps.n["gt"] = {
--   function() require("astronvim.utils.buffer").nav(-(vim.v.count > 0 and vim.v.count or 1)) end,
--   desc = "Previous buffer",
-- }
-- maps.n["]b"] =
--   { function() require("astronvim.utils.buffer").nav(vim.v.count > 0 and vim.v.count or 1) end, desc = "Next buffer" }
--
-- vim.keymap.set("n", "j", [[v:count ? 'j' : 'gj']], { noremap = true, expr = true })
-- vim.keymap.set("n", "k", [[v:count ? 'k' : 'gk']], { noremap = true, expr = true })
-- vim.keymap.set("n", "<leader>ue", "<cmd>MarkdownPreviewToggle<CR>", opt)
if vim.g.neovide then
  require "user.utils.neovide" -- Put anything you want to happen only in Neovide here
end
-- end
